<template>
  <span>
    <input
      class="input-file2"
      type="file"
      @change="exportData"
      accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
    />
    <!-- <a-button class="" @click="btnClick" :disabled="status">{{
      status ? "导入中..." : "导入常用词库"
    }}</a-button> -->
    <span class="exupload--word"  @click="btnClick" v-preventReClick>{{status ? "导入中..." : "导入模板"}}</span>
  </span>
</template>
  
  <script>
import * as XLSX from 'xlsx/xlsx.mjs'

export default {
  name: "SearchUpload",
  props: ["status"],
  methods: {
    btnClick() {
      document.querySelector(".input-file2").click();
    },
    exportData(event) {
      if (!event.currentTarget.files.length) {
        return;
      }
      const that = this;
      // 拿取文件对象
      var f = event.currentTarget.files[0];
      // 用FileReader来读取
      var reader = new FileReader();
      // 重写FileReader上的readAsBinaryString方法
      FileReader.prototype.readAsBinaryString = function (f) {
        var binary = "";
        var wb; // 读取完成的数据
        var outdata; // 你需要的数据
        var reader = new FileReader();
        reader.onload = function () {
          // 读取成Uint8Array，再转换为Unicode编码（Unicode占两个字节）
          var bytes = new Uint8Array(reader.result);
          var length = bytes.byteLength;
          for (var i = 0; i < length; i++) {
            binary += String.fromCharCode(bytes[i]);
          }
          // 接下来就是xlsx了，具体可看api
          wb = XLSX.read(binary, {
            type: "binary",
          });
          outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
          // 自定义方法向父组件传递数据
          //   console.log('outdata = ' + JSON.stringify(outdata))
          //   for(var j = 0;j<outdata.length;j++){
          //   console.log(outdata[j])
          //   }
          that.$emit("changeStatus");
          that.$emit("getResult", outdata);
          event.target.value = "";
        };
        reader.readAsArrayBuffer(f);
      };
      reader.readAsBinaryString(f);
    },
  },
};
</script>
  
  <style scoped>
.input-file2 {
  display: none !important;
}
.exupload--word{
    font-size: 16px;
    color: aqua;
    cursor: pointer;
  }
</style>
  